home *** CD-ROM | disk | FTP | other *** search
/ Suzy B Software 2 / Suzy B Software CD-ROM 2 (1994).iso / extras / programm / a56 / src / subs.c < prev    next >
C/C++ Source or Header  |  1995-04-27  |  3KB  |  166 lines

  1. /*******************************************************
  2.  *
  3.  *  a56 - a DSP56001 assembler
  4.  *
  5.  *  Written by Quinn C. Jensen
  6.  *  July 1990
  7.  *
  8.  *******************************************************\
  9.  
  10. /*
  11.  * Copyright (C) 1990-1994 Quinn C. Jensen
  12.  *
  13.  * Permission to use, copy, modify, distribute, and sell this software
  14.  * and its documentation for any purpose is hereby granted without fee,
  15.  * provided that the above copyright notice appear in all copies and
  16.  * that both that copyright notice and this permission notice appear
  17.  * in supporting documentation.  The author makes no representations
  18.  * about the suitability of this software for any purpose.  It is
  19.  * provided "as is" without express or implied warranty.
  20.  *
  21.  */
  22. static char *Copyright = "Copyright (C) 1990-1994 Quinn C. Jensen";
  23.  
  24. /*
  25.  *  subs.c - Some subroutines for the assembler.
  26.  *
  27.  */
  28.  
  29. #include "a56.h"
  30.  
  31. #define MAX 1024
  32.  
  33. char *alloc();
  34.  
  35. FILE *open_read(file)
  36. char *file;
  37. {
  38.     FILE *fp;
  39.  
  40.     if(strcmp(file, "-") == 0)
  41.         fp = stdin;
  42.     else if ((fp = fopen(file, "r")) == NULL) {
  43.         perror(file);
  44.         exit(1);
  45.     }    
  46.     return fp;
  47. }
  48.  
  49. FILE *open_write(file)
  50. char *file;
  51. {
  52.     FILE *fp;
  53.     if ((fp = fopen(file, "w")) == NULL) {
  54.         perror(file);
  55.         exit(1);
  56.     }    
  57.     return fp;
  58. }
  59.  
  60. FILE *open_append(file)
  61. char *file;
  62. {
  63.     FILE *fp;
  64.     if ((fp = fopen(file, "a")) == NULL) {
  65.         perror(file);
  66.         exit(1);
  67.     }    
  68.     return fp;
  69. }
  70.  
  71. fatal(c, a1, a2, a3, a4, a5, a6, a7, a8)
  72. char *c, *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
  73. {
  74.     fprintf(stderr, c, a1, a2, a3, a4, a5, a6, a7, a8);
  75.     exit(1);
  76. }
  77.  
  78. #define TABS 8
  79. #define MAX_BUF 256
  80.  
  81. char tabbuf[MAX_BUF], *untabn();
  82. char *untab(s)    /* expand tabs in s */
  83. register char *s;
  84. {
  85.     return untabn(s, TABS);
  86. }
  87.  
  88. char *untabn(s, stops)    /* expand tabs in s */
  89. register char *s;
  90. register int stops;
  91. {
  92.     char *o = s;
  93.  
  94.     /* copy input string into buffer to scan while input string is modified */
  95.  
  96.     register char *b = tabbuf;
  97.     
  98.     strncpy(b, s, MAX_BUF);
  99.  
  100.     /* iterate until the copy of the input string is depleted */
  101.  
  102.     while(*b) {
  103.         if(*b == '\t') {
  104.             do
  105.                 *s = ' ';
  106.             while ((++s - o) % stops);
  107.         } else {
  108.             *s = *b; s++;
  109.         }
  110.         b++;
  111.     }
  112.  
  113.     /* null terminate the resultant string */
  114.  
  115.     *s = '\0';
  116.  
  117.     return o;
  118. }
  119.  
  120. char *alloc(size)
  121. int size;
  122. {
  123.     char *p = (char *)malloc(size);
  124.     if(NOT p)
  125.         fatal("alloc:  insufficient virtual memory to allocate %d bytes\n", 
  126.             size);
  127.     return p;
  128. }
  129.  
  130. #define ascii2n(c)  \
  131.     ((c) >= 'a' ? (c) - 'a' + 10 : ((c) >= 'A' ? (c) - 'A' + 10 : (c) - '0'))
  132.  
  133. #define valid(c) ((c) >= '0' && (c) <= '9' || \
  134.     (c) >= 'A' && (c) <= 'Z' || \
  135.     (c) >= 'a' && (c) <= 'z')
  136.  
  137. strtol(s, p, base)
  138. register char *s, **p;
  139. register int base;
  140. {
  141.     register long result = 0;
  142.     register int sign = 0;
  143.  
  144.     while(*s == ' ' || *s == '\t')
  145.         s++;
  146.  
  147.     if(*s == '-') {
  148.         s++;
  149.         sign++;
  150.     }
  151.  
  152.     while(valid(*s)) {
  153.         register int dig = ascii2n(*s);
  154.         if(dig >= base)
  155.             break;
  156.         result *= base;
  157.         result += dig;
  158.          s++;
  159.     }
  160.  
  161.     if(p)
  162.         *p = s;
  163.  
  164.     return sign ? -result : result;
  165. }
  166.